<?php
namespace app\controller;

use think\facade\Filesystem;
use think\facade\Request;

class Upload
{
    public function image()
    {
        $file = Request::file('file');
        if (!$file) {
            return json(['code' => 1, 'msg' => '请选择上传文件']);
        }
        
        try {
            // 保存图片到public/uploads/exam目录
            $saveName = Filesystem::disk('public')->putFile('exam/images', $file);
            $url = '/uploads/' . $saveName;
            
            return json([
                'code' => 0,
                'msg' => '上传成功',
                'data' => [
                    'src' => $url
                ]
            ]);
        } catch (\Exception $e) {
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }
}